(require 'asdf)
;(require 'swank)
;(swank:create-server :port 4005  :dont-close t :coding-system "utf-8-unix")
(asdf:oos 'asdf:load-op 'parseltongue)


;;;
;;; Configuration
;;;

;; debug
;(setf hunchentoot:*show-lisp-errors-p* t)
;(setq hunchentoot:*catch-errors-p* nil)


(defparameter *files* '((access-log-path . #p"/home/htoot/log/access.log")
			(message-log-path . #p"/home/htoot/log/message.log")
			(cert-file . #p"/home/htoot/ssl/cert.pem")
			(key-file . #p"/home/htoot/ssl/key.pem")))


(defparameter *handle_folders* '(("/icons/"  #p"/home/htoot/htdocs/icons/")
				 ("/css/"    #p"/home/htoot/htdocs/css/")
				 ("/static/" #p"/home/htoot/htdocs/static/")))

(defparameter *blog*
  (parseltongue::make-blog
		 :short-name "vs.org"
		 :title "vs.org"
		 :subtitle "."
		 ;; logo-img-url "/icons/blog-logo.png"
		 :email-uri "mailto:blog+main(at)virtuallysecure()org"
		 :page-css-uri '(( "parseltongue" . "/css/blog.css"))
     :feed-img-uri "/icons/feed.png"
		 :root-uri "/blog/"
		 :entry-storage-path #p"/home/htoot/data/blog/entries.db"
		 :tag-stats-file-path #p"/home/htoot/data/blog/tagstats.db"
		 :ssl-port 443
		 :user-db '(("pepe" . "invalid-md5sum"))
		 :buttons '(
			    ;; (:href-url "http://weitz.de/hunchentoot/"
			    ;;  :id "hunchentoot-button"
			    ;;  :img-url "/icons/hunchentoot10.png"
			    ;;  :alt "hunchentoot")
			    ;; (:href-url "http://www.sbcl.org/"
			    ;;  :id "sbclbutton"
			    ;;  :img-url "/icons/sbclbutton.png"
			    ;;  :alt "(get 'sbcl)")
			    (:img-url "/icons/lisp110x35.png"
			     :alt "Made with Lisp")
			    (:img-url "/icons/debian88x31.png"
			     :alt "Powered by Debian")
			    )))

;;;
;;; Start HTTP Server
;;;

(defun start-ssl-server (cert-file key-file address port)
  (let ((ssl-acceptor (make-instance 'hunchentoot:ssl-acceptor
				     :ssl-privatekey-file key-file
				     :ssl-certificate-file cert-file
				     :address address
				     :port port)))
    (hunchentoot:start ssl-acceptor)
    ssl-acceptor))

(defun start-http-server (address port)
  (let ((acceptor (make-instance 'hunchentoot:acceptor :address address :port port)))
    (hunchentoot:start acceptor)
    acceptor))

(defun start-server (&key (address nil) (port 4242) (ssl-port 4243))
  
  (setf hunchentoot:*access-log-pathname* (cdr (assoc 'access-log-path *files*)))
  (setf hunchentoot:*message-log-pathname* (cdr (assoc 'message-log-path *files*)))
  
  (start-http-server address port)
  (start-ssl-server
   (cdr (assoc 'cert-file *files*))
   (cdr (assoc 'key-file *files*))
   address
   ssl-port))


; (sb-ext:save-lisp-and-die "parseltongue.core" :toplevel 'main :executable t :purify t)
; (cl+ssl:use-certificate-chain-file "/home/htoot/ssl/chain-ca.pem")

;; starts hunchentoot as user -> high ports
(start-server :address "127.0.0.1" :port 1080 :ssl-port 1443)



;;;
;;; Initialize Blog
;;;


;; initialize static folders: create folder-dispatcher for each host and folder
(dolist (folder-item *handle_folders*)
  (pushnew (hunchentoot::create-folder-dispatcher-and-handler
	    (car folder-item)
	    (cadr folder-item))
	   hunchentoot::*dispatch-table*
	   :test #'equal))

;;; init blog as server.tld/blog/
(pushnew (hunchentoot::create-prefix-dispatcher
	  "/blog"
	  (lambda () (hunchentoot:redirect "/blog/")))
	 hunchentoot::*dispatch-table* :test #'equal)


(parseltongue::init *blog*)
